//
//  main.m
//  练习城市文件处理
//
//  Created by dllo on 16/7/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "District.h"
#import "TempCIty.h"
int main(int argc, const char * argv[]) {
    NSString *path = @"/Users/dllo/DLS160710-09/Objective-C/HomeWork/练习城市文件处理/area.txt";
    NSString *areaString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *seporateredCityArray = [areaString componentsSeparatedByString:@"\n"];
    // 类方法
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSString *area in seporateredCityArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [[Province alloc] initWithName:area];
            [finalArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [finalArray lastObject];
            City *city =[[City alloc] initWithName:area];
            [pro.areaArray addObject:city];
        } else if ([area hasPrefix:@"    "]) {
            Province *pro = [finalArray lastObject];
            City *city = [pro.areaArray lastObject];
            [city.areaArray addObject:area];
        }
    }
   
    
    
    NSString *pathOfLocation = @"/Users/dllo/DLS160710-09/Objective-C/HomeWork/练习城市文件处理/Location1.txt";
    NSString *locationString = [NSString stringWithContentsOfFile:pathOfLocation encoding:NSUTF8StringEncoding error:nil];
    NSArray *seporateredAreaArray = [locationString componentsSeparatedByString:@","];
    NSMutableArray *allTempLocationArray = [NSMutableArray array];
    for (NSString *area in seporateredAreaArray) {
       NSArray *seporatoredArray = [area componentsSeparatedByString:@"|"];
        TempCIty *tempCity = [[TempCIty alloc] initWithName:[seporatoredArray firstObject] number:[seporatoredArray lastObject]];
        [allTempLocationArray addObject:tempCity];
    }
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempLocationArray sortUsingDescriptors:@[sort]];
    NSMutableArray *finalArr = [NSMutableArray array];
    for (TempCIty *tempCity in allTempLocationArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
             Province *pro = [Province tempCityWithTempCity:tempCity];
            [finalArr addObject:pro];
        } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalArr lastObject];
            City *city = [City tempCityWithTempCity:tempCity];
            [pro.areaArray addObject:city];
        } else {
            Province *pro = [finalArr lastObject];
            City *city = [pro.areaArray lastObject];
            District *district = [District tempCityWithTempCity:tempCity];
            [city.areaArray addObject:district];
        }
    }
    NSLog(@"%@", finalArr);

    return 0;
}
